<template>
  <div class="case-review-process">
    <div class="page-header">
      <h1 class="page-title">案件审查流程管理</h1>
      <div class="header-actions">
        <el-button type="success" @click="batchApprove">
          <i class="fas fa-check-circle mr-2"></i>批量通过
        </el-button>
        <el-button type="warning" @click="batchDesensitize">
          <i class="fas fa-shield-alt mr-2"></i>批量脱敏
        </el-button>
        <el-button type="primary" @click="refreshData">
          <i class="fas fa-sync-alt mr-2"></i>刷新
        </el-button>
      </div>
    </div>

    <!-- 流程统计 -->
    <div class="stats-section">
      <div class="stat-card pending">
        <div class="stat-icon">
          <i class="fas fa-clock"></i>
        </div>
        <div class="stat-content">
          <div class="stat-number">{{ stats.pendingReview }}</div>
          <div class="stat-label">待审查</div>
        </div>
      </div>
      <div class="stat-card desensitizing">
        <div class="stat-icon">
          <i class="fas fa-shield-alt"></i>
        </div>
        <div class="stat-content">
          <div class="stat-number">{{ stats.desensitizing }}</div>
          <div class="stat-label">脱敏中</div>
        </div>
      </div>
      <div class="stat-card reviewing">
        <div class="stat-icon">
          <i class="fas fa-search"></i>
        </div>
        <div class="stat-content">
          <div class="stat-number">{{ stats.reviewing }}</div>
          <div class="stat-label">人工审查</div>
        </div>
      </div>
      <div class="stat-card approved">
        <div class="stat-icon">
          <i class="fas fa-check"></i>
        </div>
        <div class="stat-content">
          <div class="stat-number">{{ stats.approved }}</div>
          <div class="stat-label">已入库</div>
        </div>
      </div>
    </div>

    <!-- 流程图 -->
    <div class="process-flow">
      <div class="flow-header">
        <h2 class="section-title">案件入库流程</h2>
      </div>
      <div class="flow-diagram">
        <div class="flow-step completed">
          <div class="step-icon">
            <i class="fas fa-gavel"></i>
          </div>
          <div class="step-content">
            <h4>案件办结</h4>
            <p>律师完成案件处理</p>
          </div>
        </div>
        <div class="flow-arrow">
          <i class="fas fa-arrow-right"></i>
        </div>
        <div class="flow-step active">
          <div class="step-icon">
            <i class="fas fa-database"></i>
          </div>
          <div class="step-content">
            <h4>自动入库</h4>
            <p>系统自动创建案例</p>
          </div>
        </div>
        <div class="flow-arrow">
          <i class="fas fa-arrow-right"></i>
        </div>
        <div class="flow-step">
          <div class="step-icon">
            <i class="fas fa-shield-alt"></i>
          </div>
          <div class="step-content">
            <h4>系统脱敏</h4>
            <p>AI自动识别敏感信息</p>
          </div>
        </div>
        <div class="flow-arrow">
          <i class="fas fa-arrow-right"></i>
        </div>
        <div class="flow-step">
          <div class="step-icon">
            <i class="fas fa-user-check"></i>
          </div>
          <div class="step-content">
            <h4>人工审查</h4>
            <p>管理员审查确认</p>
          </div>
        </div>
        <div class="flow-arrow">
          <i class="fas fa-arrow-right"></i>
        </div>
        <div class="flow-step">
          <div class="step-icon">
            <i class="fas fa-check-circle"></i>
          </div>
          <div class="step-content">
            <h4>正式入库</h4>
            <p>案例正式发布</p>
          </div>
        </div>
      </div>
    </div>

    <!-- 筛选区域 -->
    <div class="filter-section">
      <div class="filter-row">
        <el-select v-model="statusFilter" placeholder="审查状态" clearable>
          <el-option label="全部" value=""></el-option>
          <el-option label="待审查" value="pending_review"></el-option>
          <el-option label="脱敏中" value="desensitizing"></el-option>
          <el-option label="人工审查" value="manual_review"></el-option>
          <el-option label="已通过" value="approved"></el-option>
          <el-option label="已拒绝" value="rejected"></el-option>
        </el-select>
        <el-select v-model="caseTypeFilter" placeholder="案件类型" clearable>
          <el-option label="全部" value=""></el-option>
          <el-option label="交通事故" value="traffic"></el-option>
          <el-option label="合同纠纷" value="contract"></el-option>
          <el-option label="劳动纠纷" value="labor"></el-option>
          <el-option label="房产纠纷" value="property"></el-option>
        </el-select>
        <el-select v-model="urgencyFilter" placeholder="紧急程度" clearable>
          <el-option label="全部" value=""></el-option>
          <el-option label="紧急" value="urgent"></el-option>
          <el-option label="普通" value="normal"></el-option>
          <el-option label="低优先级" value="low"></el-option>
        </el-select>
        <el-date-picker
          v-model="dateRange"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          format="YYYY-MM-DD"
          value-format="YYYY-MM-DD"
        />
        <el-input
          v-model="searchQuery"
          placeholder="搜索案件标题或律师"
          class="search-input"
          clearable
        >
          <template #prefix>
            <i class="fas fa-search"></i>
          </template>
        </el-input>
      </div>
    </div>

    <!-- 案件列表 -->
    <div class="cases-section">
      <div class="section-header">
        <h2 class="section-title">待审查案件列表</h2>
        <div class="view-controls">
          <el-radio-group v-model="viewMode" size="small">
            <el-radio-button label="table">表格视图</el-radio-button>
            <el-radio-button label="card">卡片视图</el-radio-button>
          </el-radio-group>
        </div>
      </div>

      <!-- 表格视图 -->
      <div v-if="viewMode === 'table'" class="table-view">
        <el-table 
          :data="filteredCases" 
          style="width: 100%" 
          @selection-change="handleSelectionChange"
          :row-class-name="getRowClass"
        >
          <el-table-column type="selection" width="55"></el-table-column>
          <el-table-column prop="id" label="案件ID" width="80"></el-table-column>
          <el-table-column prop="title" label="案件标题" min-width="200">
            <template #default="{ row }">
              <div class="case-title">
                <span class="title-text">{{ row.title }}</span>
                <el-tag v-if="row.isUrgent" type="danger" size="small">紧急</el-tag>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="caseType" label="案件类型" width="100">
            <template #default="{ row }">
              <el-tag :type="getCaseTypeColor(row.caseType)" size="small">
                {{ getCaseTypeText(row.caseType) }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="lawyer" label="承办律师" width="100"></el-table-column>
          <el-table-column prop="completedAt" label="办结时间" width="120"></el-table-column>
          <el-table-column prop="reviewStatus" label="审查状态" width="120" align="center">
            <template #default="{ row }">
              <el-tag :type="getStatusType(row.reviewStatus)" size="small">
                {{ getStatusText(row.reviewStatus) }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="desensitizeProgress" label="脱敏进度" width="120" align="center">
            <template #default="{ row }">
              <el-progress 
                :percentage="row.desensitizeProgress" 
                :status="row.desensitizeProgress === 100 ? 'success' : null"
                :stroke-width="6"
              />
            </template>
          </el-table-column>
          <el-table-column prop="waitingDays" label="等待天数" width="100" align="center">
            <template #default="{ row }">
              <span :class="getWaitingDaysClass(row.waitingDays)">
                {{ row.waitingDays }}天
              </span>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="280" align="center">
            <template #default="{ row }">
              <el-button size="small" type="primary" @click="viewCase(row)">
                <i class="fas fa-eye mr-1"></i>查看
              </el-button>
              <el-button 
                v-if="row.reviewStatus === 'pending_review'"
                size="small" 
                type="warning" 
                @click="startDesensitize(row)"
              >
                <i class="fas fa-shield-alt mr-1"></i>开始脱敏
              </el-button>
              <el-button 
                v-if="row.reviewStatus === 'manual_review'"
                size="small" 
                type="success" 
                @click="approveCase(row)"
              >
                <i class="fas fa-check mr-1"></i>通过
              </el-button>
              <el-button 
                v-if="row.reviewStatus === 'manual_review'"
                size="small" 
                type="danger" 
                @click="rejectCase(row)"
              >
                <i class="fas fa-times mr-1"></i>拒绝
              </el-button>
              <el-button size="small" type="info" @click="viewHistory(row)">
                <i class="fas fa-history mr-1"></i>历史
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <!-- 卡片视图 -->
      <div v-else class="card-view">
        <div class="cases-grid">
          <div 
            v-for="case_ in filteredCases" 
            :key="case_.id"
            class="case-card"
            :class="getCardClass(case_)"
          >
            <div class="case-header">
              <div class="case-info">
                <h4>{{ case_.title }}</h4>
                <div class="case-meta">
                  <span>案件ID：#{{ case_.id }}</span>
                  <el-tag :type="getCaseTypeColor(case_.caseType)" size="small">
                    {{ getCaseTypeText(case_.caseType) }}
                  </el-tag>
                  <el-tag v-if="case_.isUrgent" type="danger" size="small">紧急</el-tag>
                </div>
              </div>
              <div class="case-status">
                <el-tag :type="getStatusType(case_.reviewStatus)" size="small">
                  {{ getStatusText(case_.reviewStatus) }}
                </el-tag>
              </div>
            </div>
            
            <div class="case-content">
              <div class="info-row">
                <span class="label">承办律师：</span>
                <span class="value">{{ case_.lawyer }}</span>
              </div>
              <div class="info-row">
                <span class="label">办结时间：</span>
                <span class="value">{{ case_.completedAt }}</span>
              </div>
              <div class="info-row">
                <span class="label">等待时间：</span>
                <span class="value" :class="getWaitingDaysClass(case_.waitingDays)">
                  {{ case_.waitingDays }}天
                </span>
              </div>
              <div class="info-row">
                <span class="label">脱敏进度：</span>
                <div class="progress-container">
                  <el-progress 
                    :percentage="case_.desensitizeProgress" 
                    :status="case_.desensitizeProgress === 100 ? 'success' : null"
                    :stroke-width="6"
                  />
                </div>
              </div>
            </div>
            
            <div class="case-actions">
              <el-button size="small" type="primary" @click="viewCase(case_)">
                <i class="fas fa-eye mr-1"></i>查看详情
              </el-button>
              <el-button 
                v-if="case_.reviewStatus === 'pending_review'"
                size="small" 
                type="warning" 
                @click="startDesensitize(case_)"
              >
                <i class="fas fa-shield-alt mr-1"></i>脱敏
              </el-button>
              <el-button 
                v-if="case_.reviewStatus === 'manual_review'"
                size="small" 
                type="success" 
                @click="approveCase(case_)"
              >
                <i class="fas fa-check mr-1"></i>通过
              </el-button>
              <el-button 
                v-if="case_.reviewStatus === 'manual_review'"
                size="small" 
                type="danger" 
                @click="rejectCase(case_)"
              >
                <i class="fas fa-times mr-1"></i>拒绝
              </el-button>
            </div>
          </div>
        </div>
      </div>

      <!-- 分页 -->
      <div class="pagination-section">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="totalCases"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>

    <!-- 查看案件详情对话框 -->
    <el-dialog v-model="showViewDialog" title="案件详情" width="800px">
      <div v-if="currentCase" class="case-detail">
        <div class="detail-tabs">
          <el-tabs v-model="activeTab" type="card">
            <el-tab-pane label="基本信息" name="basic">
              <div class="basic-info">
                <div class="info-grid">
                  <div class="info-item">
                    <label>案件标题：</label>
                    <span>{{ currentCase.title }}</span>
                  </div>
                  <div class="info-item">
                    <label>案件类型：</label>
                    <el-tag :type="getCaseTypeColor(currentCase.caseType)" size="small">
                      {{ getCaseTypeText(currentCase.caseType) }}
                    </el-tag>
                  </div>
                  <div class="info-item">
                    <label>承办律师：</label>
                    <span>{{ currentCase.lawyer }}</span>
                  </div>
                  <div class="info-item">
                    <label>办结时间：</label>
                    <span>{{ currentCase.completedAt }}</span>
                  </div>
                  <div class="info-item">
                    <label>案件金额：</label>
                    <span class="text-green-600 font-semibold">￥{{ currentCase.amount?.toLocaleString() }}</span>
                  </div>
                  <div class="info-item">
                    <label>审查状态：</label>
                    <el-tag :type="getStatusType(currentCase.reviewStatus)">
                      {{ getStatusText(currentCase.reviewStatus) }}
                    </el-tag>
                  </div>
                </div>
                <div class="case-summary">
                  <label>案件摘要：</label>
                  <p>{{ currentCase.summary }}</p>
                </div>
              </div>
            </el-tab-pane>
            <el-tab-pane label="脱敏信息" name="desensitize">
              <div class="desensitize-info">
                <div class="progress-section">
                  <label>脱敏进度：</label>
                  <el-progress 
                    :percentage="currentCase.desensitizeProgress" 
                    :status="currentCase.desensitizeProgress === 100 ? 'success' : null"
                  />
                </div>
                <div class="sensitive-items">
                  <label>敏感信息识别：</label>
                  <div class="items-list">
                    <div 
                      v-for="item in currentCase.sensitiveItems" 
                      :key="item.id"
                      class="sensitive-item"
                    >
                      <div class="item-content">
                        <span class="item-type">{{ item.type }}</span>
                        <span class="item-original">{{ item.original }}</span>
                        <i class="fas fa-arrow-right mx-2"></i>
                        <span class="item-masked">{{ item.masked }}</span>
                      </div>
                      <el-tag :type="item.status === 'processed' ? 'success' : 'warning'" size="small">
                        {{ item.status === 'processed' ? '已处理' : '待处理' }}
                      </el-tag>
                    </div>
                  </div>
                </div>
              </div>
            </el-tab-pane>
            <el-tab-pane label="审查历史" name="history">
              <div class="review-history">
                <el-timeline>
                  <el-timeline-item
                    v-for="record in currentCase.reviewHistory"
                    :key="record.id"
                    :timestamp="record.timestamp"
                    :type="getTimelineType(record.action)"
                  >
                    <div class="history-item">
                      <h4>{{ record.action }}</h4>
                      <p>操作人：{{ record.operator }}</p>
                      <p v-if="record.comment">备注：{{ record.comment }}</p>
                    </div>
                  </el-timeline-item>
                </el-timeline>
              </div>
            </el-tab-pane>
          </el-tabs>
        </div>
      </div>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="showViewDialog = false">关闭</el-button>
          <el-button 
            v-if="currentCase && currentCase.reviewStatus === 'manual_review'"
            type="success" 
            @click="approveCase(currentCase)"
          >
            <i class="fas fa-check mr-1"></i>通过审查
          </el-button>
          <el-button 
            v-if="currentCase && currentCase.reviewStatus === 'manual_review'"
            type="danger" 
            @click="rejectCase(currentCase)"
          >
            <i class="fas fa-times mr-1"></i>拒绝入库
          </el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 拒绝原因对话框 -->
    <el-dialog v-model="showRejectDialog" title="拒绝入库" width="500px">
      <el-form :model="rejectForm" label-width="100px">
        <el-form-item label="拒绝原因" required>
          <el-select v-model="rejectForm.reason" placeholder="选择拒绝原因">
            <el-option label="信息不完整" value="incomplete"></el-option>
            <el-option label="敏感信息未处理" value="sensitive"></el-option>
            <el-option label="案例质量不符合要求" value="quality"></el-option>
            <el-option label="重复案例" value="duplicate"></el-option>
            <el-option label="其他原因" value="other"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="详细说明">
          <el-input 
            v-model="rejectForm.comment" 
            type="textarea" 
            rows="4"
            placeholder="请输入详细的拒绝说明"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="showRejectDialog = false">取消</el-button>
          <el-button type="danger" @click="confirmReject">确认拒绝</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { ref, reactive, computed } from 'vue'

export default {
  name: 'CaseReviewProcess',
  setup() {
    const statusFilter = ref('')
    const caseTypeFilter = ref('')
    const urgencyFilter = ref('')
    const dateRange = ref([])
    const searchQuery = ref('')
    const viewMode = ref('table')
    const currentPage = ref(1)
    const pageSize = ref(20)
    const showViewDialog = ref(false)
    const showRejectDialog = ref(false)
    const currentCase = ref(null)
    const activeTab = ref('basic')
    const selectedCases = ref([])
    
    const stats = reactive({
      pendingReview: 28,
      desensitizing: 15,
      reviewing: 12,
      approved: 156
    })
    
    const rejectForm = reactive({
      reason: '',
      comment: ''
    })
    
    // 模拟案件数据
    const cases = ref([
      {
        id: 1001,
        title: '多车连环追尾交通事故赔偿案',
        caseType: 'traffic',
        lawyer: '张律师',
        completedAt: '2024-02-15',
        reviewStatus: 'pending_review',
        desensitizeProgress: 0,
        waitingDays: 3,
        isUrgent: true,
        amount: 850000,
        summary: '涉及5车连环追尾的重大交通事故，造成2人重伤，经过6个月的诉讼程序，最终达成赔偿协议...',
        sensitiveItems: [
          { id: 1, type: '身份证号', original: '320***********1234', masked: '320***********', status: 'pending' },
          { id: 2, type: '车牌号', original: '苏A12345', masked: '苏A***45', status: 'pending' },
          { id: 3, type: '手机号', original: '138****5678', masked: '138****', status: 'pending' }
        ],
        reviewHistory: [
          {
            id: 1,
            action: '案件自动入库',
            operator: '系统',
            timestamp: '2024-02-15 18:30:00',
            comment: '案件办结后自动进入审查流程'
          }
        ]
      },
      {
        id: 1002,
        title: '工伤赔偿劳动争议案',
        caseType: 'labor',
        lawyer: '李律师',
        completedAt: '2024-02-14',
        reviewStatus: 'desensitizing',
        desensitizeProgress: 65,
        waitingDays: 4,
        isUrgent: false,
        amount: 320000,
        summary: '建筑工人高空作业受伤，用人单位拒绝承认工伤关系，经过劳动仲裁和法院审理...',
        sensitiveItems: [
          { id: 1, type: '身份证号', original: '410***********5678', masked: '410***********', status: 'processed' },
          { id: 2, type: '公司名称', original: 'XX建筑有限公司', masked: '某建筑公司', status: 'processed' },
          { id: 3, type: '医院名称', original: '市第一人民医院', masked: '某三甲医院', status: 'pending' }
        ],
        reviewHistory: [
          {
            id: 1,
            action: '案件自动入库',
            operator: '系统',
            timestamp: '2024-02-14 16:20:00',
            comment: '案件办结后自动进入审查流程'
          },
          {
            id: 2,
            action: '开始系统脱敏',
            operator: '管理员',
            timestamp: '2024-02-14 17:00:00',
            comment: '启动AI脱敏处理'
          }
        ]
      },
      {
        id: 1003,
        title: '房屋买卖合同纠纷案',
        caseType: 'property',
        lawyer: '王律师',
        completedAt: '2024-02-13',
        reviewStatus: 'manual_review',
        desensitizeProgress: 100,
        waitingDays: 5,
        isUrgent: false,
        amount: 1200000,
        summary: '购房者发现房屋存在质量问题，要求退房并赔偿损失，经过调解达成和解协议...',
        sensitiveItems: [
          { id: 1, type: '身份证号', original: '110***********9876', masked: '110***********', status: 'processed' },
          { id: 2, type: '房屋地址', original: '朝阳区某某小区', masked: '某住宅小区', status: 'processed' },
          { id: 3, type: '开发商', original: 'XX房地产开发公司', masked: '某房地产公司', status: 'processed' }
        ],
        reviewHistory: [
          {
            id: 1,
            action: '案件自动入库',
            operator: '系统',
            timestamp: '2024-02-13 14:15:00',
            comment: '案件办结后自动进入审查流程'
          },
          {
            id: 2,
            action: '开始系统脱敏',
            operator: '管理员',
            timestamp: '2024-02-13 15:00:00',
            comment: '启动AI脱敏处理'
          },
          {
            id: 3,
            action: '脱敏完成',
            operator: '系统',
            timestamp: '2024-02-13 15:30:00',
            comment: 'AI脱敏处理完成，进入人工审查阶段'
          }
        ]
      }
    ])
    
    const filteredCases = computed(() => {
      return cases.value.filter(case_ => {
        const matchesStatus = !statusFilter.value || case_.reviewStatus === statusFilter.value
        const matchesType = !caseTypeFilter.value || case_.caseType === caseTypeFilter.value
        const matchesUrgency = !urgencyFilter.value || 
          (urgencyFilter.value === 'urgent' && case_.isUrgent) ||
          (urgencyFilter.value === 'normal' && !case_.isUrgent)
        const matchesSearch = !searchQuery.value || 
          case_.title.includes(searchQuery.value) ||
          case_.lawyer.includes(searchQuery.value)
        
        return matchesStatus && matchesType && matchesUrgency && matchesSearch
      })
    })
    
    const totalCases = computed(() => filteredCases.value.length)
    
    // 工具方法
    const getCaseTypeColor = (type) => {
      const colors = {
        traffic: 'danger',
        contract: 'primary',
        labor: 'warning',
        property: 'success'
      }
      return colors[type] || 'info'
    }
    
    const getCaseTypeText = (type) => {
      const texts = {
        traffic: '交通事故',
        contract: '合同纠纷',
        labor: '劳动纠纷',
        property: '房产纠纷'
      }
      return texts[type] || '其他'
    }
    
    const getStatusType = (status) => {
      const types = {
        pending_review: 'info',
        desensitizing: 'warning',
        manual_review: 'primary',
        approved: 'success',
        rejected: 'danger'
      }
      return types[status] || 'info'
    }
    
    const getStatusText = (status) => {
      const texts = {
        pending_review: '待审查',
        desensitizing: '脱敏中',
        manual_review: '人工审查',
        approved: '已通过',
        rejected: '已拒绝'
      }
      return texts[status] || '未知'
    }
    
    const getWaitingDaysClass = (days) => {
      if (days > 7) return 'text-red-600 font-bold'
      if (days > 3) return 'text-orange-600 font-semibold'
      return 'text-green-600'
    }
    
    const getRowClass = ({ row }) => {
      if (row.isUrgent) return 'urgent-row'
      if (row.waitingDays > 7) return 'overdue-row'
      return ''
    }
    
    const getCardClass = (case_) => {
      const classes = ['case-card']
      if (case_.isUrgent) classes.push('urgent-card')
      if (case_.waitingDays > 7) classes.push('overdue-card')
      return classes.join(' ')
    }
    
    const getTimelineType = (action) => {
      if (action.includes('通过') || action.includes('完成')) return 'success'
      if (action.includes('拒绝')) return 'danger'
      if (action.includes('脱敏')) return 'warning'
      return 'primary'
    }
    
    // 事件处理方法
    const handleSizeChange = (size) => {
      pageSize.value = size
      currentPage.value = 1
    }
    
    const handleCurrentChange = (page) => {
      currentPage.value = page
    }
    
    const handleSelectionChange = (selection) => {
      selectedCases.value = selection
    }
    
    const refreshData = () => {
      ElMessage.success('数据已刷新')
    }
    
    const viewCase = (case_) => {
      currentCase.value = case_
      activeTab.value = 'basic'
      showViewDialog.value = true
    }
    
    const viewHistory = (case_) => {
      currentCase.value = case_
      activeTab.value = 'history'
      showViewDialog.value = true
    }
    
    const startDesensitize = (case_) => {
      ElMessageBox.confirm(
        '确定要开始对此案件进行系统脱敏处理吗？',
        '开始脱敏',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }
      ).then(() => {
        case_.reviewStatus = 'desensitizing'
        case_.desensitizeProgress = 0
        
        // 模拟脱敏进度
        const progressTimer = setInterval(() => {
          case_.desensitizeProgress += 20
          if (case_.desensitizeProgress >= 100) {
            clearInterval(progressTimer)
            case_.reviewStatus = 'manual_review'
            case_.sensitiveItems.forEach(item => item.status = 'processed')
            ElMessage.success('脱敏处理完成，已进入人工审查阶段')
          }
        }, 1000)
        
        ElMessage.success('开始脱敏处理')
      }).catch(() => {
        ElMessage.info('已取消操作')
      })
    }
    
    const approveCase = (case_) => {
      ElMessageBox.confirm(
        '确定要通过此案件的审查并正式入库吗？',
        '通过审查',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'success',
        }
      ).then(() => {
        case_.reviewStatus = 'approved'
        case_.reviewHistory.push({
          id: case_.reviewHistory.length + 1,
          action: '审查通过',
          operator: '管理员',
          timestamp: new Date().toLocaleString(),
          comment: '案例质量符合要求，正式入库'
        })
        stats.approved++
        stats.reviewing--
        ElMessage.success('案件审查通过，已正式入库')
        showViewDialog.value = false
      }).catch(() => {
        ElMessage.info('已取消操作')
      })
    }
    
    const rejectCase = (case_) => {
      currentCase.value = case_
      showRejectDialog.value = true
    }
    
    const confirmReject = () => {
      if (!rejectForm.reason) {
        ElMessage.warning('请选择拒绝原因')
        return
      }
      
      currentCase.value.reviewStatus = 'rejected'
      currentCase.value.reviewHistory.push({
        id: currentCase.value.reviewHistory.length + 1,
        action: '审查拒绝',
        operator: '管理员',
        timestamp: new Date().toLocaleString(),
        comment: `拒绝原因：${rejectForm.reason}。${rejectForm.comment}`
      })
      
      stats.reviewing--
      
      // 重置表单
      Object.assign(rejectForm, { reason: '', comment: '' })
      
      showRejectDialog.value = false
      showViewDialog.value = false
      ElMessage.success('案件已拒绝入库')
    }
    
    const batchApprove = () => {
      if (selectedCases.value.length === 0) {
        ElMessage.warning('请先选择要批量通过的案件')
        return
      }
      
      const reviewingCases = selectedCases.value.filter(c => c.reviewStatus === 'manual_review')
      if (reviewingCases.length === 0) {
        ElMessage.warning('所选案件中没有可通过的案件')
        return
      }
      
      ElMessageBox.confirm(
        `确定要批量通过 ${reviewingCases.length} 个案件的审查吗？`,
        '批量通过',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'success',
        }
      ).then(() => {
        reviewingCases.forEach(case_ => {
          case_.reviewStatus = 'approved'
          case_.reviewHistory.push({
            id: case_.reviewHistory.length + 1,
            action: '批量审查通过',
            operator: '管理员',
            timestamp: new Date().toLocaleString(),
            comment: '批量操作通过审查'
          })
        })
        
        stats.approved += reviewingCases.length
        stats.reviewing -= reviewingCases.length
        
        ElMessage.success(`成功批量通过 ${reviewingCases.length} 个案件`)
      }).catch(() => {
        ElMessage.info('已取消操作')
      })
    }
    
    const batchDesensitize = () => {
      if (selectedCases.value.length === 0) {
        ElMessage.warning('请先选择要批量脱敏的案件')
        return
      }
      
      const pendingCases = selectedCases.value.filter(c => c.reviewStatus === 'pending_review')
      if (pendingCases.length === 0) {
        ElMessage.warning('所选案件中没有可脱敏的案件')
        return
      }
      
      ElMessageBox.confirm(
        `确定要对 ${pendingCases.length} 个案件进行批量脱敏处理吗？`,
        '批量脱敏',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }
      ).then(() => {
        pendingCases.forEach((case_, index) => {
          setTimeout(() => {
            startDesensitize(case_)
          }, index * 500) // 错开执行时间
        })
        
        ElMessage.success(`开始批量脱敏处理 ${pendingCases.length} 个案件`)
      }).catch(() => {
        ElMessage.info('已取消操作')
      })
    }
    
    return {
      statusFilter,
      caseTypeFilter,
      urgencyFilter,
      dateRange,
      searchQuery,
      viewMode,
      currentPage,
      pageSize,
      showViewDialog,
      showRejectDialog,
      currentCase,
      activeTab,
      selectedCases,
      stats,
      cases,
      rejectForm,
      filteredCases,
      totalCases,
      getCaseTypeColor,
      getCaseTypeText,
      getStatusType,
      getStatusText,
      getWaitingDaysClass,
      getRowClass,
      getCardClass,
      getTimelineType,
      handleSizeChange,
      handleCurrentChange,
      handleSelectionChange,
      refreshData,
      viewCase,
      viewHistory,
      startDesensitize,
      approveCase,
      rejectCase,
      confirmReject,
      batchApprove,
      batchDesensitize
    }
  }
}
</script>

<style scoped>
.case-review-process {
  padding: 24px;
  background-color: #f5f5f5;
  min-height: 100vh;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
}

.page-title {
  font-size: 28px;
  font-weight: bold;
  color: #1f2937;
  margin: 0;
}

.header-actions {
  display: flex;
  gap: 12px;
}

.stats-section {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 20px;
  margin-bottom: 24px;
}

.stat-card {
  background: white;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  display: flex;
  align-items: center;
  gap: 16px;
  border-left: 4px solid #e5e7eb;
}

.stat-card.pending {
  border-left-color: #6b7280;
}

.stat-card.desensitizing {
  border-left-color: #f59e0b;
}

.stat-card.reviewing {
  border-left-color: #3b82f6;
}

.stat-card.approved {
  border-left-color: #10b981;
}

.stat-icon {
  width: 50px;
  height: 50px;
  border-radius: 8px;
  background: #f3f4f6;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  color: #6b7280;
}

.stat-content {
  flex: 1;
}

.stat-number {
  font-size: 24px;
  font-weight: bold;
  color: #1f2937;
  line-height: 1;
}

.stat-label {
  font-size: 14px;
  color: #6b7280;
  margin-top: 4px;
}

/* 流程图样式 */
.process-flow {
  background: white;
  border-radius: 8px;
  padding: 24px;
  margin-bottom: 24px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.flow-diagram {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
  overflow-x: auto;
  padding: 20px 0;
}

.flow-step {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 120px;
  text-align: center;
}

.step-icon {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  margin-bottom: 12px;
  background: #f3f4f6;
  color: #9ca3af;
  border: 3px solid #e5e7eb;
}

.flow-step.completed .step-icon {
  background: #d1fae5;
  color: #065f46;
  border-color: #10b981;
}

.flow-step.active .step-icon {
  background: #dbeafe;
  color: #1e40af;
  border-color: #3b82f6;
}

.step-content h4 {
  margin: 0 0 4px 0;
  font-size: 14px;
  font-weight: 600;
  color: #1f2937;
}

.step-content p {
  margin: 0;
  font-size: 12px;
  color: #6b7280;
  line-height: 1.4;
}

.flow-arrow {
  color: #9ca3af;
  font-size: 20px;
}

.filter-section {
  background: white;
  padding: 20px;
  border-radius: 8px;
  margin-bottom: 20px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.filter-row {
  display: flex;
  gap: 16px;
  align-items: center;
  flex-wrap: wrap;
}

.search-input {
  flex: 1;
  max-width: 300px;
}

.cases-section {
  background: white;
  border-radius: 8px;
  padding: 24px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.section-title {
  font-size: 20px;
  font-weight: bold;
  color: #1f2937;
  margin: 0;
}

.case-title {
  display: flex;
  align-items: center;
  gap: 8px;
}

.title-text {
  flex: 1;
}

/* 表格行样式 */
:deep(.urgent-row) {
  background-color: #fef2f2 !important;
}

:deep(.overdue-row) {
  background-color: #fffbeb !important;
}

/* 卡片视图 */
.cases-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(400px, 1fr));
  gap: 20px;
  margin-bottom: 20px;
}

.case-card {
  background: white;
  border: 1px solid #e5e7eb;
  border-radius: 8px;
  padding: 20px;
  transition: all 0.3s ease;
}

.case-card:hover {
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  transform: translateY(-2px);
}

.case-card.urgent-card {
  border-left: 4px solid #ef4444;
}

.case-card.overdue-card {
  border-left: 4px solid #f59e0b;
}

.case-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 16px;
}

.case-info h4 {
  margin: 0 0 8px 0;
  font-size: 16px;
  font-weight: bold;
  color: #1f2937;
}

.case-meta {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
  color: #6b7280;
}

.case-content {
  margin-bottom: 16px;
}

.info-row {
  display: flex;
  align-items: center;
  margin-bottom: 8px;
}

.info-row .label {
  width: 80px;
  font-size: 14px;
  color: #6b7280;
  flex-shrink: 0;
}

.info-row .value {
  font-size: 14px;
  color: #1f2937;
}

.progress-container {
  flex: 1;
  margin-left: 8px;
}

.case-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
  flex-wrap: wrap;
}

.pagination-section {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}

/* 对话框样式 */
.case-detail {
  padding: 16px 0;
}

.basic-info {
  padding: 16px;
}

.info-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 16px;
  margin-bottom: 20px;
}

.info-item {
  display: flex;
  align-items: center;
  gap: 8px;
}

.info-item label {
  font-weight: 500;
  color: #6b7280;
  min-width: 80px;
}

.case-summary label {
  display: block;
  font-weight: 500;
  color: #6b7280;
  margin-bottom: 8px;
}

.case-summary p {
  color: #1f2937;
  line-height: 1.6;
  background: #f9fafb;
  padding: 12px;
  border-radius: 6px;
  margin: 0;
}

.desensitize-info {
  padding: 16px;
}

.progress-section {
  margin-bottom: 20px;
}

.progress-section label {
  display: block;
  font-weight: 500;
  color: #6b7280;
  margin-bottom: 8px;
}

.sensitive-items label {
  display: block;
  font-weight: 500;
  color: #6b7280;
  margin-bottom: 12px;
}

.items-list {
  space-y: 8px;
}

.sensitive-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px;
  background: #f9fafb;
  border-radius: 6px;
  margin-bottom: 8px;
}

.item-content {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 1;
}

.item-type {
  background: #e5e7eb;
  color: #374151;
  padding: 2px 8px;
  border-radius: 4px;
  font-size: 12px;
  font-weight: 500;
}

.item-original {
  color: #ef4444;
  font-family: monospace;
}

.item-masked {
  color: #10b981;
  font-family: monospace;
}

.review-history {
  padding: 16px;
}

.history-item h4 {
  margin: 0 0 4px 0;
  font-size: 14px;
  font-weight: 600;
  color: #1f2937;
}

.history-item p {
  margin: 0 0 2px 0;
  font-size: 13px;
  color: #6b7280;
}

@media (max-width: 768px) {
  .flow-diagram {
    flex-direction: column;
    gap: 20px;
  }
  
  .flow-arrow {
    transform: rotate(90deg);
  }
  
  .filter-row {
    flex-direction: column;
  }
  
  .search-input {
    max-width: 100%;
  }
  
  .cases-grid {
    grid-template-columns: 1fr;
  }
  
  .case-header {
    flex-direction: column;
    gap: 12px;
  }
  
  .info-grid {
    grid-template-columns: 1fr;
  }
}
</style>

